package com.mall.order.listener;

import com.mall.common.dto.OrderNoteDto;
import com.mall.order.dto.OrderDTO;
import com.mall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author: 谢君臣
 * @Date: 2022/8/11-18:24
 * @version: 1.0
 * @function:
 */
@Slf4j
@Component
@RabbitListener(queues = "order.release.queue")
public class OrderReleaseListener {

    @Resource
    private OrderService orderService;

    @RabbitHandler
    public void listenerOrder(OrderNoteDto order, Message message, Channel channel) throws IOException {
        try {
            orderService.closeOrder(order.getOrderSn());
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
